﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Comics.Model
{
    public sealed class Cart
    {
        public Cart()
        {
            this.Items = new List<CartItem>();
            this.Discounts = new List<Discount>();
            this.Taxes = new List<Tax>();
            this.Shipping = new List<Shipping>();
        }

        public Guid ID { get; set; }
        public string CartType { get; set; }
        public DateTime CreateDate { get; set; }
        public DateTime ExpireDate { get; set; }
        public List<CartItem> Items { get; set; }
        public List<Discount> Discounts { get; set; }
        public List<Tax> Taxes { get; set; }
        public List<Shipping> Shipping { get; set; }

        public Decimal SubTotal
        {
            get
            {
                Decimal _result = 0;
                
                foreach (CartItem _item in this.Items)
                {
                    _result += _item.SubTotal;
                }

                return _result;
            }
        }

        public decimal DiscountTotal
        {
            get
            {
                Decimal _result = 0;
                Decimal _subTotal = this.SubTotal;
                Decimal _percentage = 0;

                // Dollar discount
                foreach (Discount _discount in this.Discounts.FindAll(_x =>_x.Type == Discount.DiscountType.DollarAmount))
                {
                        _subTotal -= _discount.Amount;
                }

                foreach (Discount _discount in this.Discounts.FindAll(_x => _x.Type == Discount.DiscountType.Percentage))
                {
                    _percentage += _discount.Amount;
                }

                _result = _subTotal * _percentage;

                return Math.Round(_result, 2);
            }
        }

        public decimal ShippingTotal
        {
            get
            {
                Decimal _result = 0;
                
                foreach (Shipping _shipping in this.Shipping)
                {
                    _result += _shipping.Amount;
                }
                
                return Math.Round(_result, 2);
            }
        }

        public decimal TaxesTotal
        {
            get
            {
                Decimal _result = 0;
                Decimal _subTotal = Math.Round(this.SubTotal - this.DiscountTotal, 2);
                decimal _totalTax = 0;
                foreach (Tax _tax in this.Taxes)
                {
                    _totalTax += _tax.Amount;
                }
                _result = _subTotal * _totalTax;

                return Math.Round(_result,2);
            }
        }

        public decimal Total
        {
            get
            {
                Decimal _result = 0;

                _result = Math.Round(this.SubTotal - this.DiscountTotal + this.TaxesTotal + this.ShippingTotal, 2);

                return _result;
            }
        }
    }
}
